package com.cw1.factory.simpleFactory.order;

import com.cw1.factory.simpleFactory.factory.SimpleFactory;
import com.cw1.factory.simpleFactory.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author qiuMi
 * @description 披萨 - 订单
 * @Date 2022-10-06 23:04
 **/
public class OrderPizza {

    SimpleFactory simpleFactory;
    /**
     * 利用构造器实现逻辑 - 披萨订单
     * @return
     */
    public OrderPizza(SimpleFactory simpleFactory){
        setFactory(simpleFactory);

    }

    /**
     * 抽取工厂方法模式
     *
     * @param simpleFactory 披萨 - 工厂
     */
    private void setFactory(SimpleFactory simpleFactory) {
        do {
            String type = getType();
            Pizza pizza = simpleFactory.createPizza(type);
            if (pizza != null){
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
                System.out.println("制作披萨失败");
                break;
            }
        }while (true);
    }

    /**
     * 披萨 - 控制台输出披萨类型
     * @return 披萨 - 类型
     */
    private String getType(){
        System.out.println("请输入披萨类型：");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String type = null;
        try {
            type = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return type;
    }
}
